import { defineStore } from "pinia";
import  request  from "@/utils/request";
import { CategoryItem , ApiRes, TopCategory } from '@/types/data'
import { topCategory } from '../constants'
const defaultCategory = topCategory.map((item)=>({id: item ,name:item }))

export default defineStore('category',{
  state:()=>{
    return{ 
      list:defaultCategory as CategoryItem[],
      topCategory: {} as TopCategory,
    }
  },
  actions:{
    async getAllCategory(){
      const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
      console.log(res)
      this.list=res.data.result
    },
    async getTopCategory(id: string){
      const res = await request.get<ApiRes<TopCategory>>('/category', {
        params:{id:id}
      })
      this.topCategory = res.data.result
      console.log('getTopCategory',res)
    }
  },
  persist: {
    enabled: true,
    strategies: [{
      key: 'wjd'
    }]
  }
})